package com.cherrypt.api.tracker.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author intent
 * @date 2019/7/24 12:50
 * @about <link href='http://zzyitj.xyz/'/>
 */
public class TorrentUtils {
    private static final char[] HEX_SYMBOLS = "0123456789ABCDEF".toCharArray();

    public static byte[] hash(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest crypt;
        crypt = MessageDigest.getInstance("SHA-1");
        crypt.reset();
        crypt.update(data);
        return crypt.digest();
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_SYMBOLS[v >>> 4];
            hexChars[j * 2 + 1] = HEX_SYMBOLS[v & 0x0F];
        }
        return new String(hexChars);
    }
}
